package com.weyoung.app.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.weyoung.common.consatant.result.Result;
import com.weyoung.common.model.dto.TXOssSetDTO;
import com.weyoung.common.utils.UpLoadFiles;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author qinbo
 * @version 1.0
 * @description: TODO
 * @date 2025/4/20 22:44
 */
@RestController
@RequestMapping("/file")
@Tag(name = "APP端-文件上传")
@Slf4j
@RequiredArgsConstructor
public class FileController {
    private final UpLoadFiles upLoadFiles;
    @Value("${cos.secret-id}")
    private String secretId ;
    @Value("${cos.secret-key}")
    private String secretKey ;
    @Value("${cos.bucket-name}")
    private String bucketName ;
    @Value("${cos.endpoint}")
    private String endpoint ;
    @Value("${cos.prefix}")
    private String prefix ;

    @Operation(summary = "上传文件")
    @PostMapping("/uploadCourseImage")
    public Result<String> uploadIcon(@RequestPart("file") MultipartFile file) throws Exception {

        TXOssSetDTO dto = new TXOssSetDTO(secretId, secretKey, bucketName, endpoint, prefix);
        String name = "Course-pictures";
        String url = upLoadFiles.uploadToOss(file, dto, name);
        return Result.success(url);
    }

    public String uploadImage(MultipartFile file) throws Exception {
        TXOssSetDTO dto = new TXOssSetDTO(secretId, secretKey, bucketName, endpoint, prefix);
        String name = "Course-pictures";
        return upLoadFiles.uploadToOss(file, dto, name);
    }
}
